﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EternalRoad.Components
{
    internal sealed class Renderer : DrawableGameComponent
    {
        private WorldComponent world;
        private CameraComponent camera;
        private SpriteBatch spriteBatch;
        private Texture2D cellTextures;

        public Renderer(Game game, WorldComponent worldComponent, CameraComponent cameraComponent) 
            : base(game)
        {
            world = worldComponent;
            camera = cameraComponent;
        }

        public override void Initialize()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);

            if (world == null)
                return;

            var definitions = CellDefinitionManager.GetDefinitions();
            int size = (int)Math.Ceiling(Math.Sqrt(definitions.Count() * 3));

            Bitmap cells = new Bitmap(size * 64, size * 64);

            using (Graphics g = Graphics.FromImage(cells))
            {
                int counter = 0;

                foreach (var definition in definitions)
                {
                    int x = counter % size;
                    int y = counter / size;

                    g.DrawImage(
                        definition.Texture,
                        new System.Drawing.Rectangle(64 * x,64 * y,64,64));

                    counter++;
                    x = counter % size;
                    y = counter / size;
                }
            }

            using (MemoryStream stream = new MemoryStream())
            {
                cells.Save(stream, ImageFormat.Png);
                stream.Seek(0, SeekOrigin.Begin);
                cellTextures = Texture2D.FromStream(GraphicsDevice, stream);
            }

            int cellY = camera.ViewPort.Y * -1;
            int maxCellY = cellY + camera.ViewPort.Height;

            int cellX = camera.ViewPort.X  * -1;
            int maxCellX = cellX + camera.ViewPort.Width;

            spriteBatch.Begin();

            for (int y = cellY; y < maxCellY; y++)
            {
                for (int x = cellX; x < maxCellX; x++)
                {
                    
                    spriteBatch.Draw(
                        cellTextures, 
                        new Vector2(x, y), 
                        new Microsoft.Xna.Framework.Rectangle(0, 0, 64, 64), 
                        Microsoft.Xna.Framework.Color.White);
                }
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
